import  server  from './axios'
import { ElMessage } from 'element-plus'
let {axioscall} = server();
let base64List = [];
function createImg(dile){
    return new Promise((r,j) =>{
        var reader = new FileReader();                  //创建读取文件的对象
        reader.onload = async function (e) {
            var url = e.target.result;                  //获得url
            url = compressImg(url,0.9);
            r(url);
        }
        reader.readAsDataURL(dile);     //调用方法  
    })
}

function compressImg (base64, multiple ) {
    // 第一个参数就是需要加密的base65，
    // 第二个是压缩系数 0-1，
    return new Promise((r,j) =>{
        if (!base64) {
            return
          }
          const length = base64.length / 1024
          // 压缩方法
          let newImage = new Image()
          let quality = 0.6    // 压缩系数0-1之间
          newImage.src = base64
          newImage.setAttribute('crossOrigin', 'Anonymous') // url为外域时需要
          let imgWidth,
            imgHeight
          let w = undefined
          newImage.onload = function () {
            // 这里面的 this 指向 newImage
            // 通过改变图片宽高来实现压缩
            w = this.width * multiple
            imgWidth = this.width
            imgHeight = this.height
            let canvas = document.createElement('canvas')
            let ctx = canvas.getContext('2d')
            if (Math.max(imgWidth, imgHeight) > w) {
              if (imgWidth > imgHeight) {
                canvas.width = w
                // 等比例缩小
                canvas.height = w * (imgHeight / imgWidth)
              } else {
                canvas.height = w
                // 等比例缩小
                canvas.width = w * (imgWidth / imgHeight)
              }
            } else {
              canvas.width = imgWidth
              canvas.height = imgHeight
              // quality 设置转换为base64编码后图片的质量，取值范围为0-1  没什么压缩效果
              // 还是得通过设置 canvas 的宽高来压缩
              quality = 0.6
            }
            ctx.clearRect(0, 0, canvas.width, canvas.height)
            ctx.drawImage(this, 0, 0, canvas.width, canvas.height) //  // 这里面的 this 指向 newImage
            let smallBase64 = canvas.toDataURL('image/jpeg', quality) // 压缩语句
            r(smallBase64)
          }
    })
  }




class imgUp {
    //获取图片的base64
    getImgbase64(id){
        return new Promise(async (r, j) => {
            let file = document.getElementById(id);
            var dile = file.files;                       //在input 的files获取dom对象中内置的files属性的值是会返回一个自然下标的对象,
            for(let i= 0; i < dile.length; i++){
                base64List.push(await createImg(dile[i]))
                if(i == dile.length-1){
                    r(base64List);
                }
            }
        })
    }
    getGuid(PicData){
        return new Promise((r,j) =>{
            let par = {
                RelateType:319,
                PicData,
                RelateID:0
            }
            axioscall('PublicService.UploadPicture',par).then(res =>{
                r(res.PicGUID)
            })
        })
    }
}
let imgdata = new imgUp();
//上传图片到服务器，返回到guID
imgdata.__proto__.UpimgfileToguid = (id,flag = false) =>{
    return new Promise(async (r,j) =>{
        await imgdata.getImgbase64(id);
        let guidList = [];
        for(let i = 0; i < base64List.length; i++){
            
            guidList.push(await imgdata.getGuid(base64List[i].split(',')[1].replace(/\s/g,'')))
            if(i == base64List.length -1){
                ElMessage({
                    message: '上传成功！',
                    type: 'success',
                })
                base64List = [];
                if(!flag){
                    r(guidList[0])
                }else{
                    r(guidList);
                }   
                
            }
        }
    })
}
export default function(id){
    return imgdata
}